using System.Collections;
using System.Collections.Generic;
using PP.Basic;
using UnityEngine;

public class UIManager : UnitySingletonWithCreateMethodDontDestroyOnLoad<UIManager>
{
    Dictionary<string, MonoBehaviour> views  = new Dictionary<string, MonoBehaviour>();

    public T ShowView<T>(string path) where T : MonoBehaviour
    {
        string viewName = typeof(T).Name;

        Debug.Log($"ShowView: View ={viewName} ");
        if(views.ContainsKey(viewName))
        {
            views[viewName].gameObject.SetActive(true);
            return views[viewName] as T;
        }

        T t = UIUtil.Create<T>(path);
        t.gameObject.SetActive(true);
        views[viewName] = t;

        return t;
    }

    public void CloseView<T>()
    {
        CloseView(typeof(T).Name);
    }

    public void CloseView(string viewName)
    {
        Debug.Log($"CloseView: View ={viewName} ");
        if (views.ContainsKey(viewName))
        {
            GameObject.Destroy(views[viewName].gameObject);
            views.Remove(viewName);
            //views[viewName].gameObject.SetActive(false);
        }
    }

}
